package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.ReturnListDao;
import com.atguigu.jxc.dao.ReturnListGoodsDao;
import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.ReturnListService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReturnListServiceImpl implements ReturnListService {
    @Autowired
    private ReturnListDao returnListDao;
    @Autowired
    private ReturnListGoodsDao returnListGoodsDao;
    @Autowired
    private SupplierDao supplierDao;
    @Autowired
    private UserDao userDao;
    @Override
    public ServiceVO save(ReturnList returnList, String returnListGoodsStr) {
        String returnNumber = returnList.getReturnNumber();
        returnListDao.saveReturnListDao(returnList);
        // 使用谷歌Gson将JSON字符串数组转换成具体的集合
        Gson gson = new Gson();
        List<ReturnListGoods> list = gson.fromJson(returnListGoodsStr,new TypeToken<List<ReturnListGoods>>(){}.getType());
        for (ReturnListGoods returnListGoods : list) {
            Integer returnListId = returnList.getReturnListId();
            ReturnList returnList1=returnListDao.getReturnListByReturnNumber(returnNumber);
            returnListGoods.setReturnListId(returnList1.getReturnListId());
            returnListGoodsDao.saveReturnListGoods(returnListGoods);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> goodsList(Integer returnListId) {
        Map<String,Object> map = new HashMap<>();
        List<ReturnListGoods> returnListGoodsLists = returnListGoodsDao.getGoodsList(returnListId);
        List<Map<String,Object>> list = new ArrayList<>();
        for (ReturnListGoods returnListGoods : returnListGoodsLists) {
            Map<String,Object> innerMap = new HashMap<>();
            innerMap.put("purchaseListGoodsId",returnListGoods.getReturnListGoodsId());
            innerMap.put("goodsId",returnListGoods.getGoodsId());
            innerMap.put("goodsCode",returnListGoods.getGoodsCode());
            innerMap.put("goodsName",returnListGoods.getGoodsName());
            innerMap.put("goodsModel",returnListGoods.getGoodsModel());
            innerMap.put("goodsUnit",returnListGoods.getGoodsUnit());
            innerMap.put("goodsNum",returnListGoods.getGoodsNum());
            innerMap.put("price",returnListGoods.getPrice());
            innerMap.put("total",returnListGoods.getTotal());
            innerMap.put("returnListId",returnListGoods.getReturnListId());
            innerMap.put("goodsTypeId",returnListGoods.getGoodsTypeId());
            list.add(innerMap);
        }
        map.put("rows",list);
        return map;
    }

    @Override
    public ServiceVO deleteByReturnListId(Integer returnListId) {
        returnListDao.deleteByReturnListId(returnListId);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> list(String returnNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        Map<String,Object> map = new HashMap<>();
        List<ReturnList> returnLists=returnListDao.queryReturnList(returnNumber,supplierId,state,sTime,eTime);
        List<Map<String,Object>> list = new ArrayList<>();
        for (ReturnList returnList : returnLists) {
            Map<String,Object> innerMap = new HashMap<>();
            innerMap.put("returnListId",returnList.getReturnListId());
            innerMap.put("returnNumber",returnList.getReturnNumber());
            innerMap.put("returnDate",returnList.getReturnDate());
            innerMap.put("amountPaid",returnList.getAmountPaid());
            innerMap.put("amountPayable",returnList.getAmountPayable());
            innerMap.put("remarks",returnList.getRemarks());
            innerMap.put("state",returnList.getState());
            Integer supplierId1 = returnList.getSupplierId();
            Supplier supplier = supplierDao.getSupplierById(supplierId1);
            String supplierName = supplier.getSupplierName();
            innerMap.put("supplierName",supplierName);
            Integer userId = returnList.getUserId();
            innerMap.put("userId",userId);
            User user = userDao.getUserById(userId);
            innerMap.put("trueName",user.getTrueName());
            list.add(innerMap);
        }
        map.put("rows",list);
        return map;
    }
}
